package com.shop2cn.iapi.member.testcase;

import java.math.BigDecimal;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.member.parameter.MchMemberConfigInfoBean;
import com.shop2cn.iapi.member.parameter.MchSetOpenPointsBean;
import com.shop2cn.iapi.member.service.MchMemberConfigInfoCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.GodMemberService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.godmember.PointsRule;
import com.ymttest.database.sqlwapper.godmember.GodMemberWapper;
/**
*商家会员系统配置信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_MchMemberConfigInfo{
	private static MchMemberConfigInfoBean mchmemberconfiginfoBean;
	private static MchMemberConfigInfoCall mchmemberconfiginfoCall;
	private static Integer shopid ;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商家会员系统配置信息");
	}

	@Before
	public void caseUp() {
		mchmemberconfiginfoBean=new MchMemberConfigInfoBean();
		mchmemberconfiginfoCall=new MchMemberConfigInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(mchmemberconfiginfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_MchMemberConfigInfo_001() {
		Logger.start(true,"商家会员系统配置信息成功");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
			GodMemberService.setCollectionMobile(shopid, true);
//			Thread.sleep(10100);
			mchmemberconfiginfoBean.setShopId(shopid);
			mchmemberconfiginfoCall.setData(mchmemberconfiginfoBean);
			mchmemberconfiginfoCall.callService();			
			Logger.verifyEquals("200", mchmemberconfiginfoCall.getString("code"), "验证code");
			Logger.verifyEquals(true, mchmemberconfiginfoCall.getCollectionMobile(), "验证CollectionMobile");
			Logger.verifyEquals(false, mchmemberconfiginfoCall.getOpenPoints(), "验证getOpenPoints");
			Logger.verifyEquals(false, mchmemberconfiginfoCall.getOpenPointsOnce(), "验证getOpenPointsOnce");
			Logger.verifyEquals(false, mchmemberconfiginfoCall.getNeedInvitationCode(), "验证needInvitationCode");
			Logger.debug(FormatUtil.beanToGSONString(mchmemberconfiginfoCall.getPointsRuleList()));
			Logger.verifyEquals(true, mchmemberconfiginfoCall.getDefaultRegTerm(), "验证getDefaultRegTerm");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_MchMemberConfigInfo_002() {
		Logger.start(true,"不输入shopid失败");
		try {
			mchmemberconfiginfoCall.setData(mchmemberconfiginfoBean);
			mchmemberconfiginfoCall.callService();			
			Logger.verifyEquals("400", mchmemberconfiginfoCall.getString("code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_MchMemberConfigInfo_003() {
		Logger.start(true,"商家配置过积分规则列表成功");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
			GodMemberService.mchSetOpenPoints(shopid, true);
			GodMemberService.MchSetOpenPointsRule(GodMemberService.initMchSetOpenPointsRuleBean(shopid));
			mchmemberconfiginfoBean.setShopId(shopid);
			mchmemberconfiginfoCall.setData(mchmemberconfiginfoBean);
			mchmemberconfiginfoCall.callService();			
			Logger.verifyEquals("200", mchmemberconfiginfoCall.getString("code"), "验证code");
			Logger.verifyEquals(false, mchmemberconfiginfoCall.getCollectionMobile(), "验证CollectionMobile");
			Logger.verifyEquals(true, mchmemberconfiginfoCall.getOpenPoints(), "验证getOpenPoints");
			Logger.verifyEquals(true, mchmemberconfiginfoCall.getOpenPointsOnce(), "验证getOpenPointsOnce");
			Logger.debug(FormatUtil.beanToGSONString(mchmemberconfiginfoCall.getPointsRuleList()));
			List<PointsRule> prlist = new GodMemberWapper().selectPointsRuleByShopId(shopid);
			
			mchmemberconfiginfoCall.getPointsRuleList().forEach(x->{
				PointsRule dbp = prlist.stream().filter(y->{return y.getBizType().equals(x.getBizType());}).findFirst().get();
				Logger.verifyEquals(new BigDecimal(dbp.getPoints()).setScale(2),  new BigDecimal(x.getPoints()).setScale(2), "验证getPoints");
				Logger.verifyEquals(dbp.getThreshold().setScale(2), new BigDecimal(x.getThreshold()).setScale(2), "验证getThreshold");
			});
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_MchMemberConfigInfo_004() {
		Logger.start(true,"商家曾经配置过积分规则列表成功");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
			GodMemberService.mchSetOpenPoints(shopid, true);
			GodMemberService.MchSetOpenPointsRule(GodMemberService.initMchSetOpenPointsRuleBean(shopid));
			GodMemberService.mchSetOpenPoints(shopid, false);
			mchmemberconfiginfoBean.setShopId(shopid);
			mchmemberconfiginfoCall.setData(mchmemberconfiginfoBean);
			mchmemberconfiginfoCall.callService();			
			Logger.verifyEquals("200", mchmemberconfiginfoCall.getString("code"), "验证code");
			Logger.verifyEquals(false, mchmemberconfiginfoCall.getCollectionMobile(), "验证CollectionMobile");
			Logger.verifyEquals(false, mchmemberconfiginfoCall.getOpenPoints(), "验证getOpenPoints");
			Logger.verifyEquals(true, mchmemberconfiginfoCall.getOpenPointsOnce(), "验证getOpenPointsOnce");
			Logger.debug(FormatUtil.beanToGSONString(mchmemberconfiginfoCall.getPointsRuleList()));
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_MchMemberConfigInfo_005() {
		Logger.start(true,"商家配置过积分规则列表成功-没有config有rule");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
//			GodMemberService.mchSetOpenPoints(shopid, true);
			GodMemberService.MchSetOpenPointsRule(GodMemberService.initMchSetOpenPointsRuleBean(shopid));
			mchmemberconfiginfoBean.setShopId(shopid);
			mchmemberconfiginfoCall.setData(mchmemberconfiginfoBean);
			mchmemberconfiginfoCall.callService();			
			Logger.verifyEquals("200", mchmemberconfiginfoCall.getString("code"), "验证code");
			Logger.verifyEquals(false, mchmemberconfiginfoCall.getCollectionMobile(), "验证CollectionMobile");
			Logger.verifyEquals(false, mchmemberconfiginfoCall.getOpenPoints(), "验证getOpenPoints");
			Logger.verifyEquals(false, mchmemberconfiginfoCall.getOpenPointsOnce(), "验证getOpenPointsOnce");
			Logger.debug(FormatUtil.beanToGSONString(mchmemberconfiginfoCall.getPointsRuleList()));
			List<PointsRule> prlist = new GodMemberWapper().selectPointsRuleByShopId(shopid);
			
			mchmemberconfiginfoCall.getPointsRuleList().forEach(x->{
				PointsRule dbp = prlist.stream().filter(y->{return y.getBizType().equals(x.getBizType());}).findFirst().get();
				Logger.verifyEquals(new BigDecimal(dbp.getPoints()).setScale(2),  new BigDecimal(x.getPoints()).setScale(2), "验证getPoints");
				Logger.verifyEquals(dbp.getThreshold().setScale(2), new BigDecimal(x.getThreshold()).setScale(2), "验证getThreshold");
			});
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_MchMemberConfigInfo_006() {
		Logger.start(true,"商家会员系统配置信息成功-会员邀请");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
			GodMemberService.setCollectionMobile(shopid, true);
			new GodMemberWapper().updateMchmemberConfigNeedInvitationCode(shopid, true);
			mchmemberconfiginfoBean.setShopId(shopid);
			mchmemberconfiginfoCall.setData(mchmemberconfiginfoBean);
			mchmemberconfiginfoCall.callService();			
			Logger.verifyEquals("200", mchmemberconfiginfoCall.getString("code"), "验证code");
			Logger.verifyEquals(true, mchmemberconfiginfoCall.getCollectionMobile(), "验证CollectionMobile");
			Logger.verifyEquals(false, mchmemberconfiginfoCall.getOpenPoints(), "验证getOpenPoints");
			Logger.verifyEquals(false, mchmemberconfiginfoCall.getOpenPointsOnce(), "验证getOpenPointsOnce");
			Logger.verifyEquals(true, mchmemberconfiginfoCall.getNeedInvitationCode(), "验证needInvitationCode");
			Logger.debug(FormatUtil.beanToGSONString(mchmemberconfiginfoCall.getPointsRuleList()));
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_MchMemberConfigInfo_007() {
		Logger.start(true,"商家会员系统配置信息成功-会员邀请-bindtype");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
			GodMemberService.setCollectionMobile(shopid, true);
			GodMemberService.setInvitationCodeCall(shopid, true, 1);
			GodMemberService.setBindTypeBean(shopid, 1);
			mchmemberconfiginfoBean.setShopId(shopid);
			mchmemberconfiginfoCall.setData(mchmemberconfiginfoBean);
			mchmemberconfiginfoCall.callService();			
			Logger.verifyEquals("200", mchmemberconfiginfoCall.getString("code"), "验证code");
			Logger.verifyEquals(true, mchmemberconfiginfoCall.getCollectionMobile(), "验证CollectionMobile");
			Logger.verifyEquals(false, mchmemberconfiginfoCall.getOpenPoints(), "验证getOpenPoints");
			Logger.verifyEquals(false, mchmemberconfiginfoCall.getOpenPointsOnce(), "验证getOpenPointsOnce");
			Logger.verifyEquals(true, mchmemberconfiginfoCall.getNeedInvitationCode(), "验证needInvitationCode");
			Logger.verifyEquals(1, mchmemberconfiginfoCall.getBindType(), "验证getBindType");
			Logger.debug(FormatUtil.beanToGSONString(mchmemberconfiginfoCall.getPointsRuleList()));
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_MchMemberConfigInfo_008() {
		Logger.start(true,"商家会员系统配置信息成功-返回默认值");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
//			GodMemberService.setCollectionMobile(shopid, true);
			mchmemberconfiginfoBean.setShopId(shopid);
			mchmemberconfiginfoCall.setData(mchmemberconfiginfoBean);
			mchmemberconfiginfoCall.callService();			
			Logger.verifyEquals("200", mchmemberconfiginfoCall.getString("code"), "验证code");
			Logger.verifyEquals(false, mchmemberconfiginfoCall.getCollectionMobile(), "验证CollectionMobile");
			Logger.verifyEquals(false, mchmemberconfiginfoCall.getOpenPoints(), "验证getOpenPoints");
			Logger.verifyEquals(false, mchmemberconfiginfoCall.getOpenPointsOnce(), "验证getOpenPointsOnce");
			Logger.verifyEquals(false, mchmemberconfiginfoCall.getNeedInvitationCode(), "验证needInvitationCode");
			Logger.debug(FormatUtil.beanToGSONString(mchmemberconfiginfoCall.getPointsRuleList()));
			Logger.verifyEquals(true, mchmemberconfiginfoCall.getDefaultRegTerm(), "验证getDefaultRegTerm");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_MchMemberConfigInfo_009() {
		Logger.start(true,"商家会员系统配置信息成功-验证新增字段DefaultRegTerm");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
			GodMemberService.MchSeRegtermCall(shopid, false);
			mchmemberconfiginfoBean.setShopId(shopid);
			mchmemberconfiginfoCall.setData(mchmemberconfiginfoBean);
			mchmemberconfiginfoCall.callService();			
			Logger.verifyEquals("200", mchmemberconfiginfoCall.getString("code"), "验证code");
			Logger.verifyEquals(false, mchmemberconfiginfoCall.getCollectionMobile(), "验证CollectionMobile");
			Logger.verifyEquals(false, mchmemberconfiginfoCall.getOpenPoints(), "验证getOpenPoints");
			Logger.verifyEquals(false, mchmemberconfiginfoCall.getOpenPointsOnce(), "验证getOpenPointsOnce");
			Logger.verifyEquals(false, mchmemberconfiginfoCall.getNeedInvitationCode(), "验证needInvitationCode");
			Logger.debug(FormatUtil.beanToGSONString(mchmemberconfiginfoCall.getPointsRuleList()));
			Logger.verifyEquals(false, mchmemberconfiginfoCall.getDefaultRegTerm(), "验证getDefaultRegTerm");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}